<?php

/**
 * This is the model class for table "product_gold".
 *
 * The followings are the available columns in table 'product_gold':
 * @property string $id
 * @property string $site_key
 * @property integer $gold_game
 * @property string $gold_name
 * @property string $gold_num
 * @property string $gold_unit
 * @property string $gold_discount
 */
class ProductGold extends CActiveRecord {
    
    public static $unit_array = array(
        'K' => 'K',
        'M' => 'M',
        'G' => 'G',
    );
    
    public $show_tag_array = array(
        1 => '推荐',
        2 => '热卖'
    );

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return ProductGold the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'product_gold';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('gold_game, gold_name, gold_unit, gold_num', 'required'),
            array('gold_game, show_tag', 'numerical', 'integerOnly' => true),
            array('site_key', 'length', 'max' => 50),
            array('gold_name', 'length', 'max' => 255),
            array('gold_num, gold_unit, gold_discount', 'length', 'max' => 10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, site_key, gold_game, gold_name, gold_num, gold_unit, gold_discount, show_tag', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'site_key' => '分站',
            'gold_game' => '游戏ID',
            'gold_name' => '名称',
            'gold_num' => '金币数量',
            'gold_unit' => '单位',
            'gold_discount' => '折扣',
            'gold_update' =>'最后修改时间',
            'show_tag' => '推荐类型'
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        
        $criteria->compare('gold_game', $this->gold_game);
        $criteria->compare('gold_name', $this->gold_name, true);
        $pageSize = 30;
        if($this->gold_game)
            $pageSize = 10000;
        
        if(empty($this->site_key)){
            $criteria->addCondition("site_key=''");
        }  else {
            $criteria->compare('site_key', $this->site_key);
        }

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'sort' => array(
                        'defaultOrder' => 'gold_game ASC, gold_num ASC',
                    ),
                    'pagination' => array(
                        'pageSize' => $pageSize
                    )
                ));
    }
    
    /**
     * 生成输入框
     * @author flwof
     * @param type $name
     * @param type $id
     * @param type $val 
     */
    public static function createInput($name, $id, $val) {
        $size = strlen($val)>50 ? 50 : strlen($val);
        echo CHtml::textField("{$name}_{$id}", $val, array('rel-id' => $id, 'rel-field' => $name, 'onchange' => "updateField(this)", 'size' => $size));
    }
    /**
      +-----------------------------------------------------
     * 说明 获取金币产品
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getGoldPorduct($gid){
        $cdb = new CDbCriteria;
        $cdb->compare('site_key', Yii::app()->params->domain);
        $cdb->compare('gold_game', $gid);
        $cdb->select = "count(*) as gold_game";
        if($this->find($cdb)->gold_game>0){
            $cdb->select = "*";
            return $this->findAll($cdb);
        }else{
            $cdbc = new CDbCriteria;
            $cdbc->compare('site_key', '');
            $cdbc->compare('gold_game', $gid);
            $cdbc->order = "gold_num";
             return $this->findAll($cdbc);
        }
    }
    /**
      +-----------------------------------------------------
     * 说明 获取金币价格
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getGoldPrice($pModel,$zid){
        $num = $pModel->gold_num;
        $zModel = is_numeric($zid)?ProductCategory::model()->findByPk($zid):$zid;
        $pPrice = $zModel->price;
        $price = $num*$pPrice*$pModel->gold_discount;
        $price = WebPrice::model()->getFunctionPrice($price,$pModel->gold_game,$zModel->id,'GOLD');
        $aPrice = Yii::app()->getController()->changeCurrency($price);
        $aSave = Yii::app()->getController()->changeCurrency(($num*$pPrice)-$price);
        return $aPrice+array('save'=>$aSave['price']);
    }
    /**
      +-----------------------------------------------------
     * 说明获取打折额度 
     * @param 
     * @return 
     * @author vseach@163.com
      +-----------------------------------------------------
     * @example
     */
    public function getSaveGold($price,$pPrice,$num){
        return ($num*$pPrice)-$price;
    }
}